package cn.lagou.dw.flume.udfs;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import org.apache.hadoop.hive.ql.exec.UDF;

import java.util.ArrayList;
import java.util.List;

/**
 * Simple but deprecated way to write a Hive UDF
 *
 * @author Alex Zhang
 */
@Deprecated
public class ParseJsonIdsSimple extends UDF {

    public List<String> evaluate(String jsonStr, String arrKey) {
        if(Strings.isNullOrEmpty(jsonStr)) {
            return null;
        }
        try {
            JSONObject jsonObject = JSON.parseObject(jsonStr);
            JSONArray jsonArray = jsonObject.getJSONArray(arrKey);
            List<String> rs = new ArrayList<>();
            for (Object o : jsonArray) {
                rs.add(o.toString());
            }
            return rs;
        } catch (JSONException e) {
            return null;
        }
    }
}
